<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 5. Variable Modifiers</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="smarty.for.designers.html" title="Part II. Smarty For Template Designers">
<link rel="prev" href="language.variables.smarty.html" title="{$smarty} reserved variable">
<link rel="next" href="language.modifier.cat.html" title="cat">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 5. Variable Modifiers</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.variables.smarty.html">Prev</a> </td>
<th width="60%" align="center">Part II. Smarty For Template Designers</th>
<td width="20%" align="right"> <a accesskey="n" href="language.modifier.cat.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter" title="Chapter 5. Variable Modifiers">
<div class="titlepage"><div><div><h2 class="title">
<a name="language.modifiers"></a>Chapter 5. Variable Modifiers</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="language.modifiers.html#language.modifier.capitalize">capitalize</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.cat.html">cat</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.count.characters.html">count_characters</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.count.paragraphs.html">count_paragraphs</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.count.sentences.html">count_sentences</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.count.words.html">count_words</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.date.format.html">date_format</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.default.html">default</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.escape.html">escape</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.from_charset.html">from_charset</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.indent.html">indent</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.lower.html">lower</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.nl2br.html">nl2br</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.regex.replace.html">regex_replace</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.replace.html">replace</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.spacify.html">spacify</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.string.format.html">string_format</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.strip.html">strip</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.strip.tags.html">strip_tags</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.to_charset.html">to_charset</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.truncate.html">truncate</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.unescape.html">unescape</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.upper.html">upper</a></span></dt>
<dt><span class="sect1"><a href="language.modifier.wordwrap.html">wordwrap</a></span></dt>
</dl>
</div>
<p>
  Variable modifiers can be applied to
  <a class="link" href="language.syntax.variables.html" title="Variables">variables</a>,
  <a class="link" href="language.custom.functions.html" title="Chapter 8. Custom Functions">custom functions</a> or strings.
  To apply a modifier, specify the value followed by a <code class="literal">|</code>
  (pipe) and the modifier name. A modifier may accept additional parameters
  that affect its behavior. These parameters follow the modifier name and are
  separated by a <code class="literal">:</code> (colon). Also,
  <span class="emphasis"><em>all php-functions can be used as modifiers implicitly</em></span>
  (more below) and modifiers can be
  <a class="link" href="language.combining.modifiers.html" title="Chapter 6. Combining Modifiers">combined</a>.
 </p>
<div class="example">
<a name="id349243"></a><p class="title"><b>Example 5.1. Modifier examples</b></p>
<div class="example-contents"><pre class="programlisting">

{* apply modifier to a variable *}
{$title|upper}

{* modifier with parameters *}
{$title|truncate:40:"..."}

{* apply modifier to a function parameter *}
{html_table loop=$myvar|upper}

{* with parameters *}
{html_table loop=$myvar|truncate:40:"..."}

{* apply modifier to literal string *}
{"foobar"|upper}

{* using date_format to format the current date *}
{$smarty.now|date_format:"%Y/%m/%d"}

{* apply modifier to a custom function *}
{mailto|upper address="smarty@example.com"}

{* using  php's str_repeat *}
{"="|str_repeat:80}

{* php's count *}
{$myArray|@count}

{* this will uppercase and truncate the whole array *}
&lt;select name="name_id"&gt;
{html_options output=$my_array|upper|truncate:20}
&lt;/select&gt;

  </pre></div>
</div>
<br class="example-break"><div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
<p>
  Modifiers can be applied to any type of variables, including arrays and objects.

    </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>The default behavior was changed with Smarty 3. In Smarty 2.x, you had to
    use an "<code class="literal">@</code>" symbol to apply a modifier to an array, such as
    <code class="literal">{$articleTitle|@count}</code>. With Smarty 3, the "<code class="literal">@</code>"
    is no longer necessary, and is ignored.
    </p>
<p>
      If you want a modifier to apply to each individual item of an array, you will
      either need to loop the array in the template, or provide for this functionality
      inside your modifier function.
    </p>
</div>
<p>
    </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
        Second, in Smarty 2.x, modifiers were applied to the result of math expressions
        like <code class="literal">{8+2}</code>, meaning that <code class="literal">{8+2|count_characters}</code>
        would give <code class="literal">2</code>, as 8+2=10 and 10 is two characters long.
        With Smarty 3, modifiers are applied to the variables or atomic expressions before
        executing the calculations, so since 2 is one character long, <code class="literal">{8+2|count_characters}</code>
        gives 9. To get the old result use parentheses like <code class="literal">{(8+2)|count_characters}</code>.
      </p>
</div>
<p>
  </p>
</li>
<li class="listitem"><p>
  Modifiers are autoloaded from the <a class="link" href="variable.plugins.dir.html" title="$plugins_dir"><em class="parameter"><code>$plugins_dir</code></em></a>
  or can be registered explicitly with the <a class="link" href="api.register.plugin.html" title="registerPlugin()"><code class="varname">registerPlugin()</code></a>
  function. The later is useful for sharing a function between
   php scripts and smarty templates.
  </p></li>
<li class="listitem">
<p>
  All php-functions can be used as modifiers implicitly, as demonstrated in the
  example above.
  However, using php-functions as modifiers has two little pitfalls:
   </p>
<div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li class="listitem"><p>First - sometimes the order of the function-parameters is
   not the desirable one. Formatting <code class="literal">$foo</code>  with
   <code class="literal">{"%2.f"|sprintf:$foo}</code> actually
   works, but asks for the more intuitive, like
   <code class="literal">{$foo|string_format:"%2.f"}</code> that is provided by
   the Smarty distribution.
  </p></li>
<li class="listitem"><p>
   Secondly - if security is enabled, all php-functions that are to be used as modifiers have to be declared trusted in the
    <em class="parameter"><code>$modifiers</code></em> property of the securty policy. See the <a class="link" href="advanced.features.html#advanced.features.security" title="Security">Security</a> section for details.
  </p></li>
</ul></div>
<p>
 </p>
</li>
</ul></div>
<p>
  See also
  <a class="link" href="api.register.plugin.html" title="registerPlugin()"><code class="varname">registerPlugin()</code></a>,
  <a class="link" href="language.combining.modifiers.html" title="Chapter 6. Combining Modifiers">combining modifiers</a>.
  and
  <a class="link" href="plugins.html" title="Chapter 18. Extending Smarty With Plugins">extending smarty with plugins</a>
 </p>
<div class="sect1" title="capitalize">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.modifier.capitalize"></a>capitalize</h2></div></div></div>
<p>
     This is used to capitalize the first letter of all words in a variable.
     This is similar to the PHP <a class="ulink" href="http://php.net/ucwords" target="_top">
   <code class="varname">ucwords()</code></a> function.
  </p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Parameter Position</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">boolean</td>
<td align="center">No</td>
<td align="center"><code class="constant">FALSE</code></td>
<td>This determines whether or not words with
      digits will be uppercased</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">boolean</td>
<td align="center">No</td>
<td align="center"><code class="constant">FALSE</code></td>
<td>This determines whether or not Capital letters within words should be lowercased, e.g. "aAa" to "Aaa"</td>
</tr>
</tbody>
</table></div>
<div class="example">
<a name="id349870"></a><p class="title"><b>Example 5.2. capitalize</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

$smarty-&gt;assign('articleTitle', 'next x-men film, x3, delayed.');

?&gt;

   </pre>
<p>
    Where the template is:
   </p>
<pre class="programlisting">

{$articleTitle}
{$articleTitle|capitalize}
{$articleTitle|capitalize:true}

   </pre>
<p>
    Will output:
   </p>
<pre class="screen">

next x-men film, x3, delayed.
Next X-Men Film, x3, Delayed.
Next X-Men Film, X3, Delayed.

   </pre>
</div>
</div>
<br class="example-break"><p>See also
   <a class="link" href="language.modifier.lower.html" title="lower"><code class="varname">lower</code></a>
   and
   <a class="link" href="language.modifier.upper.html" title="upper"><code class="varname">upper</code></a>
   </p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.variables.smarty.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="smarty.for.designers.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.modifier.cat.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{$smarty} reserved variable </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> cat</td>
</tr>
</table>
</div>
</body>
</html>
